package net.hancl.modules.sys.service.impl;

import net.hancl.common.bean.page.Query;
import net.hancl.common.bean.page.QueryResult;
import net.hancl.modules.sys.bean.UserBean;
import net.hancl.modules.sys.dao.MenuMapper;
import net.hancl.modules.sys.dao.RoleMapper;
import net.hancl.modules.sys.entity.Menu;
import net.hancl.modules.sys.service.IMenuService;
import net.hancl.modules.sys.utils.UUIDUtils;
import net.hancl.modules.sys.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuService implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;

    public QueryResult<Menu> getMenuList(int page, int pagesize, MenuVo menu) {
        Query query = new Query();
        query.setBean(menu);
        query.setPageIndex(page);
        query.setPageSize(pagesize);

        // 组合分页信息
        QueryResult<Menu> queryResult = new QueryResult<Menu>();
        List<Menu> list = menuMapper.queryPages(query);


        // 总页数 和 取多少条
        queryResult.setCurrentPage(page);
        Long count = menuMapper.getCount(query);
        queryResult.setPages(count, pagesize);
        queryResult.setItems(list);

        return queryResult;
    }

    public Menu get(String id) {
        return menuMapper.get(id);
    }

    public int del(String id) {
        return menuMapper.del(id);
    }

    public int op(Menu menu, String op){
        int count = 0;
        if(op != null && op.equals("add")){
            count = add(menu);
        }else if(op != null && op.equals("update")){
            count = update(menu);
        }
        return count;
    }

    public int add(Menu menu) {
        int count = 0;
        menu.setId(UUIDUtils.getUUID());
        if(menu.getParentId() == null || menu.getParentId().equals("0")){
            menu.setParentIds("0");
            count=menuMapper.insertSelective(menu);
        }else{
            Menu m = menuMapper.get(menu.getParentId());
            menu.setParentIds(m.getParentIds()+","+m.getId());
            count=menuMapper.insertSelective(menu);
        }

        if(count>0){
            //清除权限
            //增加权限
        }
        return count;
    }

    public int update(Menu menu) {
        menu.setUpdateTime(new Date());
        return menuMapper.update(menu);
    }

    public List<Menu> getMenuListAll(String level) {
        List<Menu> list = menuMapper.queryAllByLevel(level);
        return list;
    }

    public List<Menu> getMenuListAll() {
        List<Menu> list = menuMapper.queryAll();
        return list;
    }

    //shi fou yao yu status xiang guang
    public List<Menu> queryAllMenuByCurrentName(UserBean userBean, boolean isRelStatus){
        Set<Menu> menus = new HashSet<Menu>();
        List<String> roleIds = roleMapper.queryRoleByCurrentID(userBean.getId());
        for (String roleId: roleIds) {
            List<Menu> menuList = menuMapper.getAllMenuByRoleId(roleId);
            if(isRelStatus) {
                for (Menu menu : menuList) {
                    if(menu.getStatus() == 1){
                        menu.setMenus(new ArrayList<Menu>());
                        menus.add(menu);
                    }
                }
            }else{
                for (Menu menu : menuList) {
                    menu.setMenus(new ArrayList<Menu>());
                    menus.add(menu);
                }
            }
        }
        List<Menu> list = new ArrayList<Menu>(menus);
        return list;
    }

}
